package com.ymlyj666.onlinechat.dao;

import com.ymlyj666.onlinechat.model.Message;
import com.ymlyj666.onlinechat.util.UUID;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class MessageDao extends AbstractDao<Message, String> {
    private static final String TABLE_NAME = "tb_message";

    public List<Message> findByUidAndSendTimeGT(String uid, long sendTime) {
        return execute(new MultiQueryAction() {
            @Override
            public String getSQL() {
                return "select * from " + TABLE_NAME + " where (from_uid = ? or to_uid = ?)  and send_time > ? order by send_time desc";
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                statement.setString(1, uid);
                statement.setString(2, uid);
                statement.setLong(3, sendTime);
            }
        });
    }

    @Override
    protected Message transform(ResultSet resultSet) throws SQLException {
        Message msg = new Message();
        msg.setMid(resultSet.getString("mid"));
        msg.setFromUid(resultSet.getString("from_uid"));
        msg.setToUid(resultSet.getString("to_uid"));
        msg.setSendTime(resultSet.getLong("send_time"));
        msg.setContent(resultSet.getString("content"));
        return msg;
    }

    @Override
    public Message save(Message message) {
        execute(new UpdateAction() {
            @Override
            public String getSQL() {
                if (message.getMid() == null) {
                    return "insert into " + TABLE_NAME + " values(?,?,?,?,?)";
                } else {
                    return "update " + TABLE_NAME + " set from_uid = ? ,to_uid=?,send_time=?,content=? where mid = ?";
                }
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                if (message.getMid() == null) {
                    String uuid = UUID.randomUUID().toString();
                    message.setMid(uuid);
                    message.setSendTime(System.currentTimeMillis());

                    statement.setString(1, message.getMid());
                    statement.setString(2, message.getFromUid());
                    statement.setString(3, message.getToUid());
                    statement.setLong(4, message.getSendTime());
                    statement.setString(5, message.getContent());
                } else {
                    statement.setString(1, message.getFromUid());
                    statement.setString(2, message.getToUid());
                    statement.setLong(3, message.getSendTime());
                    statement.setString(4, message.getContent());
                    statement.setString(5, message.getMid());
                }
            }
        });
        return findOne(message.getMid());
    }

    @Override
    public int delete(Message message) {
        return delete(message.getMid());
    }

    @Override
    public int delete(String s) {
        return (int) execute(new UpdateAction() {
            @Override
            public String getSQL() {
                return "delete from " + TABLE_NAME + " where mid = ?";
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                statement.setString(1, s);
            }
        });
    }

    @Override
    public Message findOne(String s) {
        return execute(new SingleQueryAction() {
            @Override
            public String getSQL() {
                return "select * from " + TABLE_NAME + " where mid = ?";
            }

            @Override
            public void setParams(PreparedStatement statement) throws SQLException {
                statement.setString(1, s);
            }
        });
    }

    @Override
    public List<Message> findAll() {
        return execute(() -> "select * from " + TABLE_NAME);
    }
}
